package com.atguigu.gulimall.search.controller;

import com.atguigu.gulimall.search.entity.vo.SearchParamVo;
import com.atguigu.gulimall.search.entity.vo.SearchResultVo;
import com.atguigu.gulimall.search.service.MallSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ProductSearchListController {

    @Autowired
    MallSearchService mallSearchService;

    //将查询参数分装成指定对象
    @RequestMapping("/list.html")
    public String listPage(SearchParamVo paramVo, Model model, HttpServletRequest servletRequest){

        String queryString = servletRequest.getQueryString();//获取所有请求参数
        paramVo.set_queryString(queryString);

        SearchResultVo result = mallSearchService.search(paramVo);
        model.addAttribute("result",result);

        return "list";
    }

    @RequestMapping("/list/json.html")
    @ResponseBody
    public SearchResultVo listPageJson(SearchParamVo paramVo){

        SearchResultVo result = mallSearchService.search(paramVo);
//        model.addAttribute("result",result);

        return result;
    }
}
